//
//  DetailViewController.m
//  Only Love MainUI
//
//  Created by xalo on 16/5/3.
//  Copyright © 2016年 赵言. All rights reserved.
//

#import "DetailViewController.h"
#import "DetailHeaderView.h"
#import "DetailCell.h"
#import "MainModel.h"

@interface DetailViewController ()

@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"DetailCell" bundle:nil] forCellReuseIdentifier:@"CELL"];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self createHeaderView];
    self.navigationItem.title = self.model.title;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
    cell.content.text = self.model.content;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CGRect titleRect = [self.model.content boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.tableView.frame)-20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil];
    return titleRect.size.height+20;
}

- (void)createHeaderView {
    
    DetailHeaderView *detilHeaderView = [[UINib nibWithNibName:@"DetailHeaderView" bundle:nil] instantiateWithOwner:nil options:nil].firstObject;
    [detilHeaderView.imageView sd_setImageWithURL:self.model.image];
    detilHeaderView.subtitle.text = self.model.subtitle;
    detilHeaderView.frame = CGRectMake(0, 0, kWidth, kHeight/2-20);
    self.tableView.tableHeaderView = detilHeaderView;
}

#pragma mark - 懒加载
//UIImage转换NSData
- (NSData *)makeImageToData:(UIImage *)image {
    
    NSData *imageData = UIImageJPEGRepresentation(image, 2.0);
    return imageData;
}

//NSData转换为UIImage
- (UIImage *)makeDataToImage:(NSData *)data{
    
    UIImage *image = [UIImage imageWithData:data];
    return image;
}

@end
